﻿using EkStudio.com.bean.xml.reflection;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.RootInterface.Field;
using EkStudio.com.bean.xml.Unit;
using System;
using System.Collections.Generic;

namespace EkStudio.com.bean.xml.BillModule
{
    public class BillUiGroupItems :BillUiFormatNode,BillUiFormatContainer
    {
        /** 分组标签 */
        private string caption = BeanUtil.FIELD_CAPTION_VALUE;
        /** 分组Key */
        private string key = "Table";
        /** 分组多语言 */
        private string languageTexts;
        private bool table = false;
        /** 分组表名 */
        private string tableName = "";
        /** 分组显示模式 */
        private string showModel = BeanUtil.TABLE_SHOWMODEL_DEFAULT;
        /** 分组是不显示 */
        private bool visible = true;
        private bool enable = true;
        /** 分组是不只读 */
        private bool readOnly = false;
        /** 分组是否允许扩展 */
        private bool expanded = false;
        /** 扩展字段 */
        private string expandField = "";
        /** 分组是否允许弹出右键菜单 */
        private bool hasPopMenu = true;
        /** 分组是否显示于查询界面 */
        private bool showQuery = true;
        private bool schemeDefaut = false;
        private int columns = 3;
        private int groupIndex = 0;
        private int location = 0;
        private int height = BeanUtil.TABLE_HEIGHT_DEFAULT;
        private int maxid = 0;

        private bool treeTable = false;
        private string treeField = "";
        private string treeCaption = "";

        private List<BillUiFieldsItem> fields;

        [Xml]
        public string Key
        {
            get
            {
                return key;
            }

            set
            {
                key = value;
            }
        }
        [Xml]
        public string Caption
        {
            get
            {
                return caption;
            }

            set
            {
                caption = value;
            }
        }
        [Xml]
        public string LanguageTexts
        {
            get
            {
                return languageTexts;
            }

            set
            {
                languageTexts = value;
            }
        }
        [Xml("isTable")]
        public bool Table
        {
            get
            {
                return table;
            }

            set
            {
                table = value;
            }
        }
        [Xml]
        public string TableName
        {
            get
            {
                return tableName;
            }

            set
            {
                tableName = value;
            }
        }
        [Xml]
        public string ShowModel
        {
            get
            {
                return showModel;
            }

            set
            {
                showModel = value;
            }
        }
        [Xml]
        public bool Visible
        {
            get
            {
                return visible;
            }

            set
            {
                visible = value;
            }
        }
        [Xml]
        public bool Enable
        {
            get
            {
                return enable;
            }

            set
            {
                enable = value;
            }
        }
        [Xml]
        public bool ReadOnly
        {
            get
            {
                return readOnly;
            }

            set
            {
                readOnly = value;
            }
        }
        [Xml]
        public bool Expanded
        {
            get
            {
                return expanded;
            }

            set
            {
                expanded = value;
            }
        }
        [Xml]
        public string ExpandField
        {
            get
            {
                return expandField;
            }

            set
            {
                expandField = value;
            }
        }
        [Xml]
        public bool HasPopMenu
        {
            get
            {
                return hasPopMenu;
            }

            set
            {
                hasPopMenu = value;
            }
        }
        [Xml]
        public bool ShowQuery
        {
            get
            {
                return showQuery;
            }

            set
            {
                showQuery = value;
            }
        }
        [Xml]
        public bool SchemeDefaut
        {
            get
            {
                return schemeDefaut;
            }

            set
            {
                schemeDefaut = value;
            }
        }
        [Xml]
        public int Columns
        {
            get
            {
                return columns;
            }

            set
            {
                columns = value;
            }
        }
        [Xml]
        public int GroupIndex
        {
            get
            {
                return groupIndex;
            }

            set
            {
                groupIndex = value;
            }
        }
        [Xml]
        public int Location
        {
            get
            {
                return location;
            }

            set
            {
                location = value;
            }
        }
        [Xml]
        public int Height
        {
            get
            {
                return height;
            }

            set
            {
                height = value;
            }
        }
        [Xml]
        public int Maxid
        {
            get
            {
                return maxid;
            }

            set
            {
                maxid = value;
            }
        }
        [Xml]
        public bool TreeTable
        {
            get
            {
                return treeTable;
            }

            set
            {
                treeTable = value;
            }
        }
        [Xml]
        public string TreeField
        {
            get
            {
                return treeField;
            }

            set
            {
                treeField = value;
            }
        }
        [Xml]
        public string TreeCaption
        {
            get
            {
                return treeCaption;
            }

            set
            {
                treeCaption = value;
            }
        }
        
        [XmlElement]
        public List<BillUiFieldsItem> Fields
        {
            get
            {
                return fields;
            }

            set
            {
                fields = value;
            }
        }

        [XmlTransient]
        public object ParentBean
        {
            get
            {
                return null;
            }

            set
            {

            }
        }

        public object Clone()
        {
            BillUiGroupItems clone = new BillUiGroupItems();
            clone.Caption = caption;
            clone.Key = key;
            clone.Table = table;
            clone.TableName = tableName;
            clone.ShowModel = showModel;
            clone.Enable = enable;
            clone.Visible = visible;
            clone.ReadOnly = readOnly;
            clone.Expanded = expanded;
            clone.Columns = columns;
            clone.GroupIndex = groupIndex;
            clone.ShowQuery = this.showQuery;
            List<BillUiFieldsItem> list = new List<BillUiFieldsItem>();
            if (Fields != null && Fields.Count > 0)
            {
                for (int i = 0; i < Fields.Count; i++)
                {
                    BillUiFieldsItem field = Fields[i];
                    Object temp = field.Clone();
                    if (temp != null)
                    {
                        list.Add((BillUiFieldsItem)temp);
                    }
                }
            }
            clone.Fields = list;

            return clone;
        }

        #region 容器功能实现

        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="node"></param>
        public void addNode(BillUiFormatNode node, BillFormatModule format)
        {
            BillUiFieldsItem field = node as BillUiFieldsItem;
            field.LanguageTexts = this.getNLanguageText(format);
            if (this.fields == null) this.fields = new List<BillUiFieldsItem>();
            this.fields.Add((BillUiFieldsItem)node);
        }

        public void removeNode(BillUiFormatNode node)
        {
            BillUiFieldsItem field = node as BillUiFieldsItem;
            if (this.fields.Contains(field))
            {
                this.fields.Remove(field);
            }
        }

        public void moveUp(BillUiFormatNode node)
        {
            BillUiFieldsItem group = node as BillUiFieldsItem;
            int index = this.fields.IndexOf(group);
            if (index != -1)
            {
                int PevIndex = index - 1;
                //logger.Info("移动前当前[" + this.fieldsProperties[index].Caption + "] 前位是[" + this.fieldsProperties[PevIndex].Caption + "]");

                this.fields.RemoveAt(index);
                this.fields.Insert(PevIndex, group);
                //logger.Info("移动后当前[" + this.fieldsProperties[index].Caption + "] 前位是[" + this.fieldsProperties[PevIndex].Caption + "]");
            }
        }

        public void moveDown(BillUiFormatNode node)
        {
            BillUiFieldsItem group = node as BillUiFieldsItem;
            int index = this.fields.IndexOf(group);
            if (index < (this.fields.Count - 1))
            {
                int LastIndex = index + 1;
                BillUiFieldsItem LastGroup = this.fields[LastIndex];
                this.fields.RemoveAt(LastIndex);
                this.fields.Insert(index, LastGroup);
            }
        }

        public string getNLanguageText(BillFormatModule format)
        {
            string result = "";
            int index = languageTexts.IndexOf("Caption");
            if (index != -1)
            {
                int length = languageTexts.Length - 7;
                result = languageTexts.Substring(0, length);
                int tempMaxid = maxid + 1;
                this.Maxid = tempMaxid;
                result = result + "fields[" + tempMaxid + "].Caption";
            }
            return result;
        }

        public void updateLanguageText(BillFormatModule format)
        {
            if (this.fields != null && this.fields.Count > 0)
            {
                this.maxid = 0;
                foreach(BillUiFieldsItem field in this.fields)
                {
                    field.LanguageTexts = this.getNLanguageText(format);
                }
            }
        }

        #endregion
    }
}
